package batch.makeFile;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.*;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.io.*;
import java.util.Date;

import batch.common.db.ConnectionManager;

public class makeUserFileForIta
{
    final String TODAY = DateFormatUtils.format(new Date(), "yyyyMMdd");

    public makeUserFileForIta() {
        ;
    }

    public static void main(String [] args) {
        Logger.getLogger("makefile.result").info("( * ) Start.");

        makeUserFileForIta mkRus = new makeUserFileForIta();
        mkRus.startMake();

        Logger.getLogger("makefile.result").info("( * ) End.");
    }

    //Make a log file and a data file.
    public void startMake(){
        Connection   conn = null;
        Statement    stmt = null;
        ResultSet    rs   = null;
        ResultSetMetaData  rsmd = null;
        OutputStreamWriter oswFile = null;

        //String fileNm  = "/home01/sfc/batch/makeFile/data/ita_TIM_"  + TODAY + ".txt";
        String fileNm  = "/home01/sfc/batch/makeFile/data/bra_"  + TODAY + ".txt";

        int cnt = 0;
        int rsCnt = 0;

        StringBuffer sbTemp = new StringBuffer();

        StringBuffer sQuery = new StringBuffer()
                             .append("SELECT ID                                    \n")
                             .append("     , FIRSTNAME                             \n")
                             .append("     , LASTNAME                              \n")
                             .append("     , GENDER                                \n")
                             //.append("     , BIRTH                                 \n")
                             //.append("     , EMAIL                                 \n")
                             //.append("     , NEWS_LETTER                           \n")
                             //.append("     , HP_BRAND                              \n")
                             //.append("     , HP_MODEL                              \n")
                             //.append("     , HP_IMEI                               \n")
                             .append("     , HP_NUM                                \n")
                             //.append("     , HP_PROVIDER                           \n")
                             //.append("     , HP_BUY_YM                             \n")
                             //.append("     , GRADE                                 \n")
                             //.append("     , TO_CHAR(CREATED, 'YYYYMMDDHH24MISS')  \n")
                             //.append("     , TO_CHAR(MODIFIED, 'YYYYMMDDHH24MISS') \n")
                             //.append("     , TO_CHAR(DELETED, 'YYYYMMDDHH24MISS')  \n")
                             //.append("     , POINT                                 \n")
                             //.append("     , POWER                                 \n")
                             //.append("     , HP_SN                                 \n")
                             //.append("     , RESERVED3                             \n")
                             .append("  FROM G_MB_MEMBER                           \n")
                             //.append(" WHERE REG_COUNTRY = 'ita'                   \n")
                             //.append("   AND NEWS_LETTER = 'Y'                     \n")
                             //.append("   AND HP_PROVIDER = '0104403'               \n");

                             .append(" WHERE REG_COUNTRY = 'bra'                   \n")
                             .append("   AND RESERVED3 = 'Y'                     \n")
                             .append("   AND DELETED IS NULL               \n");

        try {
            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sQuery.toString());

            oswFile = new OutputStreamWriter(new FileOutputStream(fileNm, false), "ISO-8859-1");

            sbTemp.append("ID"         ).append("|")
                  .append("FIRST NAME" ).append("|")
                  .append("LAST NAME"  ).append("|")
                  .append("GENDER"     ).append("|")
                  //.append("BIRTHDAY"   ).append("|")
                  //.append("EMAIL"      ).append("|")
                  //.append("NEWS_LETTER").append("|")
                  //.append("HP_BRAND"   ).append("|")
                  //.append("HP_MODEL"   ).append("|")
                  //.append("HP_IMEI"    ).append("|")
                  .append("HP_NUM"     ).append("|");
                  //.append("HP_PROVIDER").append("|")
                  //.append("HP_BUY_YM"  ).append("|")
                  //.append("GRADE"      ).append("|")
                  //.append("CREATED"    ).append("|")
                  //.append("MODIFIED"   ).append("|")
                  //.append("DELETED"    ).append("|")
                  //.append("POINT"      ).append("|")
                  //.append("POWER"      ).append("|")
                  //.append("HP_SN"      ).append("|")
                  //.append("RESERVED3"  ).append("\n");

            oswFile.write(sbTemp.toString());

            while(rs.next()){
                rsmd = rs.getMetaData();
                rsCnt = rsmd.getColumnCount();

                sbTemp.setLength(0);

                for(int i=1;i<=rsCnt;i++) {
                    sbTemp.append(ObjectUtils.toString(rs.getObject(i), ""));

                    if( i == rsCnt ){
                        sbTemp.append("\n");
                    }
                    else{
                        sbTemp.append("|");
                    }
                }

                oswFile.write(sbTemp.toString());
                cnt++;

                if(cnt % 500 == 0){
                    Logger.getLogger("makefile.result").info("here we are == " + cnt);
                }
            }

            Logger.getLogger("makefile.result").info("Total Count : " + cnt);
        }
        catch(Exception ex) {
            ex.printStackTrace();

            Logger.getLogger("makefile.result").info(ex.toString());
            StackTraceElement[] stElement = ex.getStackTrace();
            for(int p=0; p<stElement.length;p++){
                Logger.getLogger("makefile.result").info(stElement[p].toString());
            }
        }
        finally {
            if (rs   != null) try { rs.close(); }   catch (Exception e) { e.printStackTrace(); }
            if (stmt != null) try { stmt.close(); } catch (Exception e) { e.printStackTrace(); }
            if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
            if (oswFile != null) try { oswFile.close(); } catch (Exception e) { e.printStackTrace(); }
        }
    }
}